package cn.tedu.jws.admin.service.impl;

/**
 * 业主业务逻辑实现类
 */


import cn.tedu.jws.admin.ex.ServiceException;
import cn.tedu.jws.admin.mapper.OwnerMapper;
import cn.tedu.jws.admin.pojo.dto.OwnerAddNewDTO;
import cn.tedu.jws.admin.pojo.dto.OwnerUpdateDTO;
import cn.tedu.jws.admin.pojo.entity.Owner;
import cn.tedu.jws.admin.pojo.vo.OwnerListVO;
import cn.tedu.jws.admin.pojo.vo.OwnerStandardVO;
import cn.tedu.jws.admin.service.OwnerService;
import cn.tedu.jws.admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OwnerServiceImpl implements OwnerService {

    @Autowired
    private OwnerMapper ownerMapper;

    /**
     * 添加业主信息
     * @param ownerAddNewDTO
     */
    @Override
    public void addNew(OwnerAddNewDTO ownerAddNewDTO) {
        log.debug("开始处理业主业务，业主信息为：{}",ownerAddNewDTO);
        //判断业主是否存在
        String name = ownerAddNewDTO.getName();
        int count = ownerMapper.countByName(name);
        if (count==1){
          String message = "添加业主失败,业主已存在";
          log.warn(message);
          throw  new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }


        //添加业主
        Owner owner = new Owner();
        BeanUtils.copyProperties(ownerAddNewDTO,owner);
        //判断业主传入的性别
        String sex = ownerAddNewDTO.getSex();
        if ("男".equals(sex)){
            owner.setSex(1);
        }else {
            owner.setSex(0);
        }
        //生成一个业主随机标识
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        owner.setUserType(s);


        int rows = ownerMapper.insert(owner);
        if (rows!=1){
            String message = "添加业主失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }


    }

    /**
     * 删除业主
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 判断业主是否存在
        OwnerStandardVO query = ownerMapper.getStandardById(id);
        if (query == null) {
            String message = "删除业主失败,业主不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        //删除业主
        int rows = ownerMapper.deleteById(id);
        if (rows!=1){
            String message = "删除业主失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    /**
     * 通过ID查询业主详情
     * @param id
     * @return
     */
    @Override
    public OwnerStandardVO getStandardById(Long id) {
        // 判断业主是否存在
        OwnerStandardVO query = ownerMapper.getStandardById(id);
        if (query == null) {
            String message = "删除业主失败,业主不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        return ownerMapper.getStandardById(id);
    }

    /**
     * 查询业主列表
     * @return
     */
    @Override
    public List<OwnerListVO> list() {
        List<OwnerListVO> list = ownerMapper.list();
        return list;
    }

    /**
     * 修改业主信息
     * @param id
     * @param ownerUpdateDTO
     */
    @Override
    public void updateInfoById(Long id, OwnerUpdateDTO ownerUpdateDTO) {
        //查询该业主是否存在
        OwnerStandardVO query = ownerMapper.getStandardById(id);
        if (query == null) {
            String message = "修改业主失败,业主不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        //判断名字是否冲突
        String name = ownerUpdateDTO.getName();
        int count = ownerMapper.countByName(name);
        if (count>0){
            String message = "修改业主失败,业主已存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        //判断身份证和电话号码是否冲突
        String card = ownerUpdateDTO.getCard();
        String phone = ownerUpdateDTO.getPhone();
        int countByCardAndPhone = ownerMapper.countByCardAndPhone(card, phone);
        if (countByCardAndPhone>0){
            String message = "修改业主失败,身份证或电话已占用，请重新输入";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        //修改业主信息
        Owner owner = new Owner();
        owner.setId(id);
        BeanUtils.copyProperties(ownerUpdateDTO,owner);
        int rows = ownerMapper.update(owner);
        if (rows != 1){
            String message = "修改业主失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


}
